package talvan;

import java.util.ArrayList;
import javax.swing.JDialog;
import javax.swing.JOptionPane;


public class GUIMain extends javax.swing.JFrame {

    private DataHandler data;
    private SQLCallerMain caller;
    
    public GUIMain(DataHandler data) {
        initComponents();
        this.data = data;
        caller = data.getMainCaller();
        populateFundaList();
        populateEventList();
    }
    
    
    /**
     * Metodir hjá Fundir
     * 
     */
    
    /**
     * Metoda sum fyllir fundaLista við fundum, sum brúkarin er innkalaður til
     */
    private void populateFundaList()
    {
        try{
            ArrayList<Meeting> list = caller.getMeetings();
            fundaList.setListData(list.toArray());
            
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Catched í GUI / populate fundalist");
        }
    }
    
    /**
     * metoda sum fyllir feltini út, við valdu fundum
     */
    private void selectFund()
    {
        Meeting fund = (Meeting)fundaList.getSelectedValue();
        fundaTextLabel.setText(fund.getName());
        fundaTextArea.setText(fund.getDescription());   
        String temp = fund.getTime().toString();
        fundurTimeLabel.setText(""+temp.substring(0, 19));
       
    }
    
    /**
     * metoda sum opnar nýtt panel við øllum lutakarum til hendan fundin
     */
    private void openMeeting()
    {
        Meeting selected = (Meeting)fundaList.getSelectedValue();
        
        JDialog jd = new JDialog(this, "Selected Fundur", true);
        jd.setContentPane(new GUIParticipantsList(jd, selected.getMeetingID(), data.getSQLCallerParticipantsList()));
        jd.pack();
        jd.setVisible(true);
    }
    
    /**
     * 
     * Metoda til Button tá man skal stovna ein nýggjan fund
     */
    private void newMeeting()
    {
        JDialog jd = new JDialog(this, "Nýggjan Fund", true);
        jd.setContentPane(new GUINewMeeting(data.getSQLCallerNewMeeting(),jd));
        jd.pack();
        jd.setVisible(true);
    }
    
    /**
     * Metodir hjá Titak
     * 
     */
    private void populateEventList()
    {
        try{
            ArrayList<Event> list = caller.getEvents();
            eventList.setListData(list.toArray());
            
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Catched í GUI / populate TiltakList");
            
        }
    }
    
    /**
     * Metoda sum fyllir event listan við øllum tí sum mann hevur fingi inngjóðing til
     */
    private void selectEvent()
    {
        Event event = (Event)eventList.getSelectedValue();
        
        eventTextLabel.setText(event.getName());
        eventTekstArea.setText(event.getDescription());   
        //String temp = event.getTime().toString();
        //fundurTimeLabel.setText(""+temp.substring(0, 19));
        
        //getInvited
        populateEventEmployeList();
    }
    
    /**
     * Metoda sum fyllir listan við eventemploye list, við tí sum man hevur valt í combobox
     */
    private void populateEventEmployeList()
    {
        Event event = (Event)eventList.getSelectedValue();
        //øll
        if(eventCombobox.getSelectedIndex() == 1)
        {
            //System.out.println("number of invited " + caller.getInvited(event.getEventID()));
            //eventEmployeList.setListData(caller.getInvited(event.getEventID()).toArray());
        }
        //øll jalig svar
        if(eventCombobox.getSelectedIndex() == 2)
        {
            //eventEmployeList.setListData(caller.getInvited(event.getEventID()).toArray());
        }
        //øll nei ligsvar
        if(eventCombobox.getSelectedIndex() == 3)
        {
            //eventEmployeList.setListData(caller.getInvited(event.getEventID()).toArray());
        }
        // og øll sum ikki vita enn
        if(eventCombobox.getSelectedIndex() == 4)
        {
            //eventEmployeList.setListData(caller.getInvited(event.getEventID()).toArray());
        }
    }
    
    
    
    /**
     * metoda sum opnar GUINewEvent panle
     */
    private void newTiltak()
    {
        JDialog jd = new JDialog(this, "Nýtt Tiltak", true);
        jd.setContentPane(new GUINewEvent(data.getSQLCallerNewEvent(),jd));
        jd.pack();
        jd.setVisible(true);
    }
    
    
    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup6 = new javax.swing.ButtonGroup();
        Tiltøk = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        fundaList = new javax.swing.JList();
        newMeetingButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        fundaTextLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        fundaTextArea = new javax.swing.JTextArea();
        fundurTimeLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        eventList = new javax.swing.JList();
        jSeparator2 = new javax.swing.JSeparator();
        eventTextLabel = new javax.swing.JLabel();
        newTiltakButton = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        eventTekstArea = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        acceptRadioButton = new javax.swing.JRadioButton();
        declineRadioButton = new javax.swing.JRadioButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        eventEmployeList = new javax.swing.JList();
        jLabel8 = new javax.swing.JLabel();
        eventTime = new javax.swing.JLabel();
        eventCombobox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Komandi Fundir");

        fundaList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Fundur 1", "Fundur 2", "Fundur 3", "Fundur 4", "Fundur 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        fundaList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fundaListMouseClicked(evt);
            }
        });
        fundaList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                fundaListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(fundaList);

        newMeetingButton.setText("Nýggjan Fund");
        newMeetingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMeetingButtonActionPerformed(evt);
            }
        });

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        fundaTextLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        fundaTextLabel.setText("[Navn á Fundi]");

        fundaTextArea.setColumns(20);
        fundaTextArea.setEditable(false);
        fundaTextArea.setRows(5);
        fundaTextArea.setText("Informatión Fyri valda Fundin");
        jScrollPane2.setViewportView(fundaTextArea);

        fundurTimeLabel.setText("[klokkan og Dato vanda Fundi]");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1)
                    .addComponent(newMeetingButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(fundaTextLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(fundurTimeLabel)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(fundaTextLabel)
                            .addComponent(fundurTimeLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1)
                        .addGap(18, 18, 18)
                        .addComponent(newMeetingButton))
                    .addComponent(jSeparator1))
                .addContainerGap())
        );

        Tiltøk.addTab("Fundir", jPanel1);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("Kjak");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addContainerGap(632, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addContainerGap(303, Short.MAX_VALUE))
        );

        Tiltøk.addTab("Kjak", jPanel2);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Komandi Tiltøk");

        eventList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Tiltak 1", "Tiltak 2", "Tiltak 3", "Tiltak 4", "Tiltak 5", "Tiltak 6" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        eventList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                eventListValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(eventList);

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        eventTextLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        eventTextLabel.setText("[Navn á Tiltaki]");

        newTiltakButton.setText("Nýtt Tiltak");
        newTiltakButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newTiltakButtonActionPerformed(evt);
            }
        });

        eventTekstArea.setColumns(20);
        eventTekstArea.setEditable(false);
        eventTekstArea.setRows(5);
        jScrollPane4.setViewportView(eventTekstArea);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel7.setText("Svar uppá Invitatión");

        buttonGroup6.add(acceptRadioButton);
        acceptRadioButton.setText("Ja");

        buttonGroup6.add(declineRadioButton);
        declineRadioButton.setText("Nei");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addComponent(acceptRadioButton, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(declineRadioButton)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(acceptRadioButton)
                    .addComponent(declineRadioButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addContainerGap())
        );

        eventEmployeList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Starvsfólk 1", "Starvsfólk 2", "Starvsfólk 3", "Starvsfólk 4", "Starvsfólk 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane5.setViewportView(eventEmployeList);

        jLabel8.setText("Lutakarir");

        eventTime.setText("[Klokkan]");

        eventCombobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Invitera", "Ja", "Nei", "veit ikki" }));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                    .addComponent(newTiltakButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(eventTextLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(eventTime))
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(eventCombobox, 0, 127, Short.MAX_VALUE)
                    .addComponent(jScrollPane5))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator2)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(newTiltakButton)
                        .addGap(7, 7, 7))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(eventTextLabel)
                            .addComponent(eventTime)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(jScrollPane4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(eventCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );

        Tiltøk.addTab("Tiltøk", jPanel4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Tiltøk)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Tiltøk)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void newTiltakButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newTiltakButtonActionPerformed
        newTiltak();
    }//GEN-LAST:event_newTiltakButtonActionPerformed

    private void fundaListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_fundaListValueChanged
        selectFund();
    }//GEN-LAST:event_fundaListValueChanged

    private void newMeetingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMeetingButtonActionPerformed
        newMeeting();
    }//GEN-LAST:event_newMeetingButtonActionPerformed

    private void fundaListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fundaListMouseClicked
        if(evt.getClickCount() == 2)
        {
            openMeeting();
        }
    }//GEN-LAST:event_fundaListMouseClicked

    private void eventListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_eventListValueChanged
        selectEvent();
    }//GEN-LAST:event_eventListValueChanged

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane Tiltøk;
    private javax.swing.JRadioButton acceptRadioButton;
    private javax.swing.ButtonGroup buttonGroup6;
    private javax.swing.JRadioButton declineRadioButton;
    private javax.swing.JComboBox eventCombobox;
    private javax.swing.JList eventEmployeList;
    private javax.swing.JList eventList;
    private javax.swing.JTextArea eventTekstArea;
    private javax.swing.JLabel eventTextLabel;
    private javax.swing.JLabel eventTime;
    private javax.swing.JList fundaList;
    private javax.swing.JTextArea fundaTextArea;
    private javax.swing.JLabel fundaTextLabel;
    private javax.swing.JLabel fundurTimeLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JButton newMeetingButton;
    private javax.swing.JButton newTiltakButton;
    // End of variables declaration//GEN-END:variables

}
